package mortal.game.control
{
    import Message.Game.*;
    import Message.Public.*;
    import modules.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.pet.*;
    import mortal.game.view.pet.info.*;
    import mortal.game.view.pet.skill.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class PetController extends Controller
    {
        private var _petModule:PetModule;
        private var _petViewWindow:PetViewWindow;
        private var _petSkillRefreshWindow:PetSkillRefreshWindow;
        private var _petSkillBatchRefreshWindow:BatchRefreshWindow;
        private var _petSkillForgetWindow:PetSkillForgetWindow;
        private static var _isShowResumeTip:Boolean = true;
        public static var isBatchRefresh:Boolean = false;

        public function PetController()
        {
            Dispatcher.addEventListener(EventName.PetBattle, this.onPetBattle);
            Dispatcher.addEventListener(EventName.PetBattleByUid, this.onPetBattleByUid);
            Dispatcher.addEventListener(EventName.PetRest, this.onPetRest);
            Dispatcher.addEventListener(EventName.PetRelease, this.onPetRelease);
            Dispatcher.addEventListener(EventName.PetStatusChange, this.onPetStatusChange);
            Dispatcher.addEventListener(EventName.PetModify, this.onPetModify);
            Dispatcher.addEventListener(EventName.PetModifySuccess, this.onPetModifySuccess);
            Dispatcher.addEventListener(EventName.PetBattleSuccess, this.onPetBattleSuccessHandler);
            Dispatcher.addEventListener(EventName.PetRestSuccess, this.onPetRestSuccessHandler);
            Dispatcher.addEventListener(EventName.PetDeath, this.onPetDeathHandler);
            Dispatcher.addEventListener(EventName.PetStatusUpdate, this.onPetStatusUpdateHandler);
            Dispatcher.addEventListener(PetEventName.PetFeedClick, this.onFeedClickHandler);
            Dispatcher.addEventListener(PetModule.AutoFeedPet, this.onAutoFeedPetHandler);
            Dispatcher.addEventListener(EventName.Guide_PetCall, this.onGuidePetCall);
            Dispatcher.addEventListener(EventName.Guide_PetRefresh, this.onGuidePetRefresh);
            Dispatcher.addEventListener(EventName.PetBagCapacityChange, this.onPetBagCapacityChange);
            Dispatcher.addEventListener(EventName.PetFeedInPack, this.onPetFeedInPackHandler);
            Dispatcher.addEventListener(EventName.PetViewInfo, this.onPetViewInfoHandler);
            Dispatcher.addEventListener(EventName.PetMeetLookOverPlayerPet, this.onPetMeetLookOverPlayerPetHandler);
            Dispatcher.addEventListener(EventName.PetShowInfo, this.onPetShowInfoHandler);
            Dispatcher.addEventListener(EventName.PetBagUpdate, this.onPetBagUpdateHandler);
            Dispatcher.addEventListener(EventName.PetStrengThenCant, this.onPetStrengThenCantHandler);
            Dispatcher.addEventListener(EventName.PetLearnSkill, this.onPetLearnSkillHandler);
            Dispatcher.addEventListener(EventName.PetLearnSkillSuccess, this.onPetLearnSkillSuccessHandler);
            Dispatcher.addEventListener(EventName.PetUpdateSkill, this.onPetUpdateSkillHandler);
            Dispatcher.addEventListener(EventName.PetUpdateSkillSuccess, this.onPetUpdateSkillSuccessHandler);
            Dispatcher.addEventListener(EventName.PetSkillOpenRefreshWindow, this.onPetSkillOpenRefreshWindowHandler);
            Dispatcher.addEventListener(PetEventName.PetBatchRefreshWindow, this.onPetBatchRefreshWindowHandler);
            Dispatcher.addEventListener(EventName.PetGetSkillProp, this.onPetGetSkillPropHandler);
            Dispatcher.addEventListener(EventName.PetGetSkillPropSuccess, this.onPetGetSkillPropSuccessHandler);
            Dispatcher.addEventListener(EventName.PetRandSkillProp, this.onPetRandSkillPropHandler);
            Dispatcher.addEventListener(EventName.PetRandSkillPropSuccess, this.onPetRandSkillPropSuccessHandler);
            Dispatcher.addEventListener(EventName.PetMakeSkillProp, this.onPetMakeSkillPropHandler);
            Dispatcher.addEventListener(EventName.PetMakeSkillPropSuccess, this.onPetMakeSkillPropSuccessHandler);
            Dispatcher.addEventListener(PetEventName.PetOpenForgetSkillWindow, this.onPetOpenForgetSkillWindowHandler);
            Dispatcher.addEventListener(EventName.PetForgetSkill, this.onPetForgetSkillHandler);
            Dispatcher.addEventListener(EventName.PetForgetSkillSuccess, this.onPetForgetSkillSuccessHandler);
            Dispatcher.addEventListener(EventName.PetDigSkill, this.onPetDigSkillHandler);
            Dispatcher.addEventListener(EventName.PetDigSkillSuccess, this.onPetDigSkillSuccessHandler);
            Dispatcher.addEventListener(EventName.PetChangePetModel, this.onPetChangePetModelHandler);
            Dispatcher.addEventListener(EventName.PetChangePetModelSuccess, this.onPetChangePetModelSuccessHandler);
            Dispatcher.addEventListener(EventName.PetOpenSkillStudyTab, this.onPetOpenSkillStudyTabHandler);
            
        }

        override protected function initServer() : void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.PetBagMsg, this.onPetBagGet);
            NetDispatcher.addCmdListener(ServerCommand.LookupPetReplySuccess, this.onLookupPetReplySuccess);
            
        }

        override protected function initView() : IView
        {
            if (this._petModule == null)
            {
                this._petModule = new PetModule();
                this._petModule.addEventListener(WindowEvent.SHOW, this.onPetWindowShowHandler);
                this._petModule.addEventListener(WindowEvent.CLOSE, this.onPetWindowCloseHandler);
            }
            return this._petModule;
        }

        override public function get view() : IView
        {
            return this.initView();
        }

        private function onPetWindowShowHandler(event:WindowEvent) : void
        {
            this._petModule.initPetPanel(cache.pet.petInfos);
            Dispatcher.dispatchEvent(new DataEvent(PetEventName.PetShowToTab, 0));
            this.onGuidePetCall(null);
            this.onGuidePetRefresh(null);
            
        }

        private function onPetWindowCloseHandler(event:WindowEvent) : void
        {
            if (cache.guide.petGuide || cache.guide.guidePetRefresh)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetWindowClose, PetModule.currentPet));
            }
            
        }

        public function onPetBagGet(param1:SPetBag) : void
        {
            var _loc_2:* = cache.pet.getBattlePet();
            if (_loc_2 != null)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetBattleSuccess, {uid:_loc_2.uid, entityId:cache.pet.entityId}));
            }
            
        }

        public function onPetBagUpdateHandler(event:DataEvent) : void
        {
            var _loc_3:String = null;
            var _loc_2:* = event.data as SPetUpdate;
            if (_loc_2 && _loc_2.updateType != 4 && _loc_2.bagType == EPlayerItemPosType._EPlayerItemPosTypePet)
            {
                _loc_3 = PetColor.getColorName(_loc_2.petInfo);
                if (_loc_2.updateType == 1)
                {
                    if (_loc_3 != "")
                    {
                        MsgManager.addBroadCast("获得一只" + _loc_3 + "宠物");
                        MsgManager.addTipText("获得一只" + _loc_3 + "宠物");
                    }
                }
                else if (_loc_2.updateType == 2)
                {
                    if (this._petModule && this._petModule.isHide == false)
                    {
                        if (_loc_3 != "")
                        {
                            MsgManager.addBroadCast("丢弃一只" + _loc_3 + "宠物");
                            MsgManager.addTipText("丢弃一只" + _loc_3 + "宠物");
                        }
                    }
                }
            }
            if (this._petModule && this._petModule.isHide == false)
            {
                this._petModule.updatePet(_loc_2);
            }
            
        }

        private function onPetStatusChange(event:DataEvent) : void
        {
            var _loc_2:* = event.data["uid"] as String;
            var _loc_3:* = new EPetStatus(event.data["status"] as int);
            GameProxy.petProxy.changePetStatus(_loc_2, _loc_3);
            
        }

        private function onPetBattle(event:DataEvent) : void
        {
            var petInfo:SPetInfo;
            var dataEvent:* = event;
            petInfo = dataEvent.data as SPetInfo;
            if (petInfo.status == EPetStatus._EPetSire)
            {
                MsgManager.addBroadCast("宠物正在繁殖中，不能出战！");
                
            }
            if (petInfo.status != EPetStatus._EPetRest && petInfo.status != EPetStatus._EPetEquip)
            {
                MsgManager.addBroadCast("宠物已经出战！");
                
            }
            if (petInfo.life == 0)
            {
                MsgManager.addBroadCast("宠物血量为0，不能召唤，请补充血量后再召唤！");
                
            }
            if (petInfo.energy <= 0)
            {
                MsgManager.addBroadCast("宠物精力等于0，不能召唤，请补充精力后再召唤！");
                
            }
            if (RolePlayer.instance.isDoubleRest)
            {
                ScreenOperation.interruptDoubleRest(function () : void
            {
                petBattle(petInfo);
                
            }
            );
                
            }
            if (Cache.instance.fish.isInFishing)
            {
                ScreenOperation.interruptFish(function () : void
            {
                petBattle(petInfo);
                
            }
            );
                
            }
            if (Cache.instance.fish.isInRoastFish)
            {
                ScreenOperation.interruptRoastFish(function () : void
            {
                petBattle(petInfo);
                
            }
            );
                
            }
            this.petBattle(petInfo);
            
        }

        private function petBattle(param1:SPetInfo) : void
        {
            PetModule.needCallPetUid = param1.uid;
            GameProxy.petProxy.activatePet(PetModule.needCallPetUid);
            
        }

        private function onPetBattleByUid(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            var _loc_3:* = cache.pet.getPetInfoByUid(_loc_2);
            if (_loc_3)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetBattle, _loc_3));
            }
            
        }

        private function onPetRest(event:DataEvent) : void
        {
            if (cache.pet.battlePet)
            {
                GameProxy.petProxy.deactivatePet(cache.pet.battlePet.uid);
            }
            
        }

        private function onPetRelease(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            GameProxy.petProxy.firePet(_loc_2);
            
        }

        private function onPetModify(event:DataEvent) : void
        {
            var _loc_2:* = event.data["uid"] as String;
            var _loc_3:* = event.data["petName"] as String;
            GameProxy.petProxy.changePetName(_loc_2, _loc_3);
            
        }

        private function onPetModifySuccess(event:DataEvent) : void
        {
            MsgManager.addBroadCast("修改宠物名称成功！");
            
        }

        private function onPetBattleSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["uid"] as String;
            cache.pet.battlePet = cache.pet.getPetInfoByUid(_loc_2);
            MsgManager.addBroadCast("成功召唤宠物");
            
        }

        private function onPetRestSuccessHandler(event:DataEvent) : void
        {
            cache.pet.battlePet = null;
            
        }

        private function onPetDeathHandler(event:DataEvent) : void
        {
            cache.pet.battlePet = null;
            if (this._petModule)
            {
            }
            
        }

        private function onPetStatusUpdateHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SPetStatusUpdate;
            if (this._petModule)
            {
                this._petModule.updatePetStatus(_loc_2);
            }
            
        }

        private function onFeedClickHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["petInfo"] as SPetInfo;
            var _loc_3:* = event.data["feedType"] as String;
            var _loc_4:* = cache.pack.backPackCache.getPetDrugItem(_loc_2, _loc_3);
            if (cache.pack.backPackCache.getPetDrugItem(_loc_2, _loc_3))
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.FeedPet, {itemData:_loc_4, amount:1, petUid:_loc_2.uid}));
            }
            else if (_loc_3 == FeedType.Energy)
            {
                MsgManager.addBroadCast("背包中没有宠物恢复精力的玄精丹");
            }
            else if (_loc_3 == FeedType.Life)
            {
                MsgManager.addBroadCast("背包中没有宠物恢复生命的药品");
            }
            else if (_loc_3 == FeedType.Mana)
            {
                MsgManager.addBroadCast("背包中没有宠物恢复法力的药品");
            }
            else if (_loc_3 == FeedType.LifeSpan)
            {
                MsgManager.addBroadCast("背包中没有宠物恢复寿命的药品");
            }
            
        }

        private function onAutoFeedPetHandler(event:DataEvent) : void
        {
            var _loc_5:ItemData = null;
            var _loc_6:String = null;
            var _loc_2:* = event.data["petInfo"] as SPetInfo;
            var _loc_3:* = event.data["feedType"] as String;
            var _loc_4:* = event.data["effectType"] as String;
            if (event.data["effectType"] as String == PetDrugEffect.Duration)
            {
                _loc_5 = cache.pack.backPackCache.getPetDrugItem(_loc_2, _loc_3);
            }
            if (_loc_5)
            {
                _loc_6 = _loc_5.uid;
                Dispatcher.dispatchEvent(new DataEvent(EventName.FeedPet, {itemData:_loc_5, amount:1, petUid:_loc_2.uid}));
                _isShowResumeTip = true;
            }
            else if (_isShowResumeTip)
            {
                MsgManager.addBroadCast("背包中没有宠物恢复的药品");
                _isShowResumeTip = false;
            }
            
        }

        private function onGuidePetCall(event:DataEvent) : void
        {
            if (cache.guide.petGuide)
            {
                this._petModule.guidePetCall();
            }
            
        }

        private function onGuidePetRefresh(event:DataEvent) : void
        {
            if (cache.guide.guidePetRefresh)
            {
                this._petModule.guidePetRefresh();
            }
            
        }

        private function onPetStrengThenCantHandler(event:DataEvent) : void
        {
            if (cache.guide.guidePetRefresh && this._petModule && !this._petModule.isHide)
            {
                this._petModule.hide();
            }
            
        }

        private function onPetBagCapacityChange(event:DataEvent) : void
        {
            if (this._petModule)
            {
                this._petModule.changePetCapacity();
            }
            
        }

        private function onPetFeedInPackHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["itemData"] as ItemData;
            var _loc_3:* = event.data["amount"] as int;
            var _loc_4:* = cache.pet.battlePet;
            if (_loc_2)
            {
                if (_loc_4)
                {
                    if (_loc_2.itemInfo.item.level > _loc_4.level)
                    {
                        MsgManager.addBroadCast("药品等级过高，不能使用");
                        
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.FeedPet, {itemData:_loc_2, amount:_loc_3, petUid:_loc_4.uid}));
                }
                else
                {
                    MsgManager.addBroadCast("需要宠物出战才能使用");
                }
            }
            
        }

        private function onPetViewInfoHandler(event:DataEvent) : void
        {
            var _loc_2:* = cache.role.entityInfo.entityId;
            var _loc_3:* = event.data as SEntityId;
            GameProxy.playerInfoProxy.lookupRequest_async(_loc_2, _loc_3);
            
        }

        private function onPetMeetLookOverPlayerPetHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["playerId"] as int;
            var _loc_3:* = event.data["petUid"] as String;
            GameProxy.petProxy.lookOverPlayerPet(_loc_2, _loc_3);
            
        }

        private function onPetShowInfoHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SPetInfo;
            this.onLookupPetReplySuccess(_loc_2);
            
        }

        private function onLookupPetReplySuccess(param1:SPetInfo) : void
        {
            if (param1)
            {
                if (this._petViewWindow == null)
                {
                    this._petViewWindow = new PetViewWindow();
                }
                this._petViewWindow.updatePetInfoPanel(param1);
                if (this._petModule && this._petModule.isHide == false)
                {
                    this._petViewWindow.show(this._petModule.x + 30, this._petModule.y);
                }
                else
                {
                    this._petViewWindow.show();
                }
            }
            
        }

        private function onPetLearnSkillHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["uid"] as String;
            var _loc_3:* = event.data["skillId"] as int;
            GameProxy.petProxy.learnSkill(_loc_2, _loc_3);
            
        }

        private function onPetLearnSkillSuccessHandler(event:DataEvent) : void
        {
            MsgManager.addBroadCast("学习技能成功");
            
        }

        private function onPetUpdateSkillHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["uid"] as String;
            var _loc_3:* = event.data["oldSkillId"] as int;
            GameProxy.petProxy.updateSkill(_loc_2, _loc_3);
            
        }

        private function onPetUpdateSkillSuccessHandler(event:DataEvent) : void
        {
            MsgManager.addBroadCast("升级技能成功");
            
        }

        private function onPetSkillOpenRefreshWindowHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            if (this._petSkillRefreshWindow == null)
            {
                this._petSkillRefreshWindow = new PetSkillRefreshWindow();
            }
            this._petSkillRefreshWindow.itemData = _loc_2;
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetGetSkillProp, _loc_2.uid));
            this._petSkillRefreshWindow.show();
            if (this._petSkillBatchRefreshWindow)
            {
                this._petSkillBatchRefreshWindow.hide();
            }
            
        }

        private function onPetBatchRefreshWindowHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["itemData"] as ItemData;
            var _loc_3:* = event.data["skills"] as Array;
            var _loc_4:* = event.data["allTimes"] as int;
            if (this._petSkillBatchRefreshWindow == null)
            {
                this._petSkillBatchRefreshWindow = new BatchRefreshWindow();
            }
            this._petSkillBatchRefreshWindow.itemData = _loc_2;
            this._petSkillBatchRefreshWindow.skills = _loc_3;
            this._petSkillBatchRefreshWindow.allTimes = _loc_4;
            this._petSkillBatchRefreshWindow.show();
            if (this._petSkillRefreshWindow)
            {
                this._petSkillRefreshWindow.hide();
            }
            
        }

        private function onPetGetSkillPropHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            GameProxy.petProxy.getPetSkillProp(_loc_2);
            
        }

        private function onPetGetSkillPropSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["skills"] as Array;
            var _loc_3:* = event.data["allTimes"] as int;
            if (this._petSkillRefreshWindow)
            {
                this._petSkillRefreshWindow.skills = _loc_2;
                this._petSkillRefreshWindow.allTimes = _loc_3;
            }
            
        }

        private function onPetRandSkillPropHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["uid"] as String;
            var _loc_3:* = event.data["unit"] as int;
            var _loc_4:* = event.data["times"] as int;
            GameProxy.petProxy.randPetSkillProp(_loc_2, _loc_3, _loc_4);
            
        }

        private function onPetRandSkillPropSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["skills"] as Array;
            var _loc_3:* = event.data["allTimes"] as int;
            if (isBatchRefresh)
            {
                if (this._petSkillBatchRefreshWindow)
                {
                    this._petSkillBatchRefreshWindow.skills = _loc_2;
                    this._petSkillBatchRefreshWindow.allTimes = _loc_3;
                }
            }
            else if (this._petSkillRefreshWindow)
            {
                this._petSkillRefreshWindow.skills = _loc_2;
                this._petSkillRefreshWindow.allTimes = _loc_3;
            }
            
        }

        private function onPetMakeSkillPropHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["uid"] as String;
            var _loc_3:* = event.data["skillId"] as int;
            GameProxy.petProxy.makePetSkillProp(_loc_2, _loc_3);
            
        }

        private function onPetMakeSkillPropSuccessHandler(event:DataEvent) : void
        {
            if (this._petSkillBatchRefreshWindow)
            {
                this._petSkillBatchRefreshWindow.clear();
                this._petSkillBatchRefreshWindow.hide();
            }
            if (this._petSkillRefreshWindow)
            {
                this._petSkillRefreshWindow.clear();
                this._petSkillRefreshWindow.hide();
            }
            
        }

        private function onPetOpenForgetSkillWindowHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["petInfo"] as SPetInfo;
            var _loc_3:* = event.data["skillInfo"] as SPetSkill;
            if (this._petSkillForgetWindow == null)
            {
                this._petSkillForgetWindow = new PetSkillForgetWindow();
            }
            this._petSkillForgetWindow.updateWindow(_loc_2, _loc_3);
            this._petSkillForgetWindow.show();
            
        }

        private function onPetForgetSkillHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["uid"] as String;
            var _loc_3:* = event.data["skillId"] as int;
            GameProxy.petProxy.forgetPetSkill(_loc_2, _loc_3);
            
        }

        private function onPetForgetSkillSuccessHandler(event:DataEvent) : void
        {
            if (this._petSkillForgetWindow)
            {
                this._petSkillForgetWindow.hide();
            }
            MsgManager.addBroadCast("宠物遗忘技能成功");
            
        }

        private function onPetDigSkillHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["petUid"] as String;
            var _loc_3:* = event.data["uid"] as String;
            var _loc_4:* = event.data["skillId"] as int;
            GameProxy.petProxy.digPetSkill(_loc_2, _loc_3, _loc_4);
            
        }

        private function onPetDigSkillSuccessHandler(event:DataEvent) : void
        {
            MsgManager.addBroadCast("封印宠物技能成功");
            
        }

        private function onPetChangePetModelHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["petMainUid"] as String;
            var _loc_3:* = event.data["petDeputyUid"] as String;
            var _loc_4:* = event.data["uid"] as String;
            GameProxy.petProxy.changePetModel(_loc_2, _loc_3, _loc_4);
            
        }

        private function onPetChangePetModelSuccessHandler(event:DataEvent) : void
        {
            MsgManager.addBroadCast("宠物化形成功");
            
        }

        private function onPetOpenSkillStudyTabHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            (this.view as PetModule).show();
            this._petModule.gotoStudySkillPanel(_loc_2);
            
        }

    }
}
